package com.company.controller;

import com.company.common.R;
import com.company.entity.Address;
import com.company.entity.Yaopinfenlei;
import com.company.entity.Yonghu;

import com.company.service.AddressService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cz_
 * @description
 * @date 2024/6/17
 */

@RestController
public class addressController {
    @Autowired
    AddressService addressService;

    /*
    * 地址分页查询
    * */
    @PostMapping("/selectaddress/{page}")
        public R SelectAddress(@PathVariable("page") int page){
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,5);
        List<Address> list=this.addressService.list();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
    *地址修改
    * */
    @PostMapping("/updateaddress")
    public R UpdateAddress(@RequestBody Address address){
        R r = new R();
        boolean b = this.addressService.updateById(address);
        if (!b){
            r.setCode(100);
        }

        return r;
    }

    /*
    * 根据id查询地址信息
    * */
    @PostMapping("/selectaddressid")
    public R SelectAddressId(@RequestBody Address address){
        Address byId = this.addressService.getById(address.getId());
        return new R().add("byId",byId);
    }

    /*
    * 地址删除
    * */
    @RequestMapping("/deleteaddressid")
    public R del(@RequestBody Address address){
        boolean remove = this.addressService.removeById(address);
        R r=new R();
        if(!remove){
            r.setCode(100);
        }
        return r;
    }
}